package com.gc.business.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gc.business.domain.IntegralDetail;
import com.gc.business.domain.IntegralGoods;
import com.gc.business.domain.IntegralOrder;
import com.gc.business.mapper.IntegralDetailMapper;
import com.gc.business.mapper.IntegralOrderMapper;
import com.gc.business.service.IIntegralOrderService;
import com.gc.common.utils.IdUtils;
import com.gc.common.utils.StringUtils;
import com.gc.system.domain.SysUser;
import com.gc.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 积分订单 服务层实现
 *
 * @author liujinteng
 * @date 2021-01-04
 */
@Slf4j
@Service
public class IntegralOrderServiceImpl extends ServiceImpl<IntegralOrderMapper, IntegralOrder> implements IIntegralOrderService {

    @Autowired
    private SysUserMapper userMapper;

    private final IntegralDetailMapper integralDetailMapper;

    @Autowired
    public IntegralOrderServiceImpl(IntegralDetailMapper integralDetailMapper) {
        this.integralDetailMapper = integralDetailMapper;
    }

    @Override
    public List<IntegralOrder> list(IntegralOrder integralOrder) {
        LambdaQueryWrapper<IntegralOrder> wrapper = new LambdaQueryWrapper<>();
        if (integralOrder != null) {
            if (StringUtils.isNotEmpty(integralOrder.getOrderId())) {
                wrapper.eq(IntegralOrder::getOrderId, integralOrder.getOrderId());
            }
            if (StringUtils.isNotEmpty(integralOrder.getGoodsId())) {
                wrapper.eq(IntegralOrder::getGoodsId, integralOrder.getGoodsId());
            }
            if (StringUtils.isNotEmpty(integralOrder.getUserId())) {
                wrapper.eq(IntegralOrder::getUserId, integralOrder.getUserId());
            }
            if (StringUtils.isNotEmpty(integralOrder.getUserName())) {
                wrapper.like(IntegralOrder::getUserName, integralOrder.getUserName());
            }
            if (StringUtils.isNotEmpty(integralOrder.getOrderNum())) {
                wrapper.eq(IntegralOrder::getOrderNum, integralOrder.getOrderNum());
            }
            if (StringUtils.isNotEmpty(integralOrder.getExchangeCount())) {
                wrapper.eq(IntegralOrder::getExchangeCount, integralOrder.getExchangeCount());
            }
            if (StringUtils.isNotEmpty(integralOrder.getPayIntegral())) {
                wrapper.eq(IntegralOrder::getPayIntegral, integralOrder.getPayIntegral());
            }
            if (StringUtils.isNotEmpty(integralOrder.getExchangeTime())) {
                wrapper.eq(IntegralOrder::getExchangeTime, integralOrder.getExchangeTime());
            }
            if (StringUtils.isNotEmpty(integralOrder.getExchangeStatus())) {
                wrapper.eq(IntegralOrder::getExchangeStatus, integralOrder.getExchangeStatus());
            }
            if (StringUtils.isNotEmpty(integralOrder.getCreateBy())) {
                wrapper.like(IntegralOrder::getCreateBy, integralOrder.getCreateBy());
            }
            if (StringUtils.isNotEmpty(integralOrder.getCreateTime())) {
                wrapper.eq(IntegralOrder::getCreateTime, integralOrder.getCreateTime());
            }
            if (StringUtils.isNotEmpty(integralOrder.getUpdateBy())) {
                wrapper.like(IntegralOrder::getUpdateBy, integralOrder.getUpdateBy());
            }
            if (StringUtils.isNotEmpty(integralOrder.getUpdateTime())) {
                wrapper.eq(IntegralOrder::getUpdateTime, integralOrder.getUpdateTime());
            }
            if (StringUtils.isNotEmpty(integralOrder.getRemark())) {
                wrapper.eq(IntegralOrder::getRemark, integralOrder.getRemark());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrder(SysUser sysUser, IntegralGoods integralGoods, int exchangeCount) {
        //保存订单
        IntegralOrder integralOrder = new IntegralOrder();
        integralOrder.setGoodsId(integralGoods.getGoodsId());
        integralOrder.setUserId(sysUser.getUserId());
        integralOrder.setUserName(sysUser.getUserName());
        integralOrder.setOrderNum(IdUtils.getPrimaryKey().toString());
        integralOrder.setExchangeCount(exchangeCount);
        integralOrder.setPayIntegral(integralGoods.getExchangeScore());
        integralOrder.setExchangeTime(DateUtil.now());
        integralOrder.setExchangeStatus("0");
        int row = baseMapper.insert(integralOrder);
        //减用户积分
        BigDecimal remainIntegral = NumberUtil.sub(sysUser.getOwnScore(), integralOrder.getPayIntegral().toString());
        sysUser.setOwnScore(remainIntegral.stripTrailingZeros().toPlainString());
        userMapper.updateUser(sysUser);
        //生成明细
        IntegralDetail integralDetail = new IntegralDetail();
        integralDetail.setDetailTitle("兑换礼品");
        integralDetail.setUserId(sysUser.getUserId());
        integralDetail.setScoreType("1");
        integralDetail.setChangeScore("-" + integralOrder.getPayIntegral());
        integralDetail.setCreateBy(sysUser.getUserName());
        integralDetail.setCreateTime(DateUtil.date());
        integralDetailMapper.insert(integralDetail);
        if (row > 0) {
            return true;
        }
        return false;
    }
}
